import {getRealEstate, getFiles, downloadFiles,getAssetRegistrationList} from '@/views/property-info-sys/proprietary-information/api/propertietar.js'
import {checkFiles} from '@/api/operate-construction-contract.js'
import governmentInvestment from '../component/government-investment-projects'
import {getAllProject} from "@/views/property-info-sys/project-manage/api/project_manage"
import relevancyProperty from '../component/relevancy-property'

export default {
    components: {
        relevancyProperty,governmentInvestment
    },
    computed: {
        // this.proprietaryTypes = getDict('proprietary_types')
        // this.planUses = getDict('plan_uses')
        // this.landUses = getDict('land_uses')
        /*proprietaryType: function () {
            let types =getDict('proprietary_types')
            let pv = this.propForm.cardType
            if(pv && types.length > 0){
                const pts = types.find(t => t.value == pv)
                return pts ? pts.label : ''
            }
            return ''
        },
        landType: function() {
            let types =getDict('land_uses')
            let pv = this.propForm.landType
            if(pv && types.length > 0){
                const pts = types.find(t => t.value == pv)
                return pts ? pts.label : ''
            }
            return ''
        },
        planningType: function() {
            let types =getDict('plan_uses')
            let pv = this.propForm.planningType
            if(pv && types.length > 0){
                const pts = types.find(t => t.value == pv)
                return pts ? pts.label : ''
            }
            return ''
        },*/
        project: function () {
            if(this.projects){
                let obj = this.projects.find(pd => pd.projectId == this.propForm.projectId )
                if(obj){
                    return obj.projectName
                }
            }
        }
    },
    mounted() {
        const loading = this.$loading({
            lock: true,
            text: '数据加载中'
          });
        getRealEstate(this.$route.params.id).then(rep => {
            loading.close()
            this.propForm = rep.data.data
            for (let key in this.propForm) {
                if (this.propForm[key] === null || this.propForm[key] === '') {
                    this.propForm[key] = '--'
                }
            }
            if (this.propForm.assetRegistrationId !== '1') {
                this.governmentInvestmentFlag = true
                this.$refs.governmentInvestment.handleChangeAssetRegistration(this.propForm.assetRegistrationId)
            } else {
                this.governmentInvestmentFlag = false
            }
            //获取省市县区下拉数据
            this.queryProvince();
            if (this.propForm.provinceCode) {
                this.queryCitys(this.propForm.provinceCode)
            }
            if (this.propForm.cityCode) {
                this.queryCounty(this.propForm.cityCode)
            }


        }).catch( e=>{
            loading.close()
        })
        getFiles(this.$route.params.id, this.model).then(rep => {
            this.fileList = rep.data.data
        })

        // getAllProject().then(rep => {
        //     this.projects =   rep.data.data
        // })
        getAllProject().then(rep => {
            let projectData = rep.data.data
            if(projectData){
                this.projects = projectData.map( p => {
                    return {label: p.projectName, value: p.projectId}
                })
            }
        })
        getAssetRegistrationList().then(rep => {
            let projectData = rep.data.data
            if(projectData){
                this.assetRegistrationTypes = projectData.map( p => {
                    return {label: p.projectName, value: p.assetRegistrationId}
                })
            }
        })


    },
    created() {
        this.proprietaryTypes = this.getDict('asset-property-right-form')
        this.planUses = this.getDict('plan_uses')
        this.landUses = this.getDict('land_uses')
        this.cardTypes = this.getDict('real_estate_card_type')
        this.assetClassificationTypes = this.getDict('asset-classification')
        this.useStatusTypes = this.getDict('asset-use-status')
        this.acquisitionMethodTypes = this.getDict('asset-acquisition-method')
        this.valueTypes = this.getDict('asset-value-type')
        this.ownershipCertificateTypes = this.getDict('asset-ownership-certificate')
        this.ownershipNatureTypes = this.getDict('asset-ownership-nature')
        this.buildingStructureTypes = this.getDict('asset-building-structure')
        this.depreStatusTypes = this.getDict('asset_depreciation_status')
        if (this.proprietaryTypes.length > 0 && this.proprietaryTypes !== null) {
            for (let i = 0; i < this.proprietaryTypes.length; i++) {
                this.proprietaryTypes[i].value = parseInt(this.proprietaryTypes[i].value)
            }
        }
        if (this.assetClassificationTypes.length > 0 && this.assetClassificationTypes !== null) {
            for (let i = 0; i < this.assetClassificationTypes.length; i++) {
                this.assetClassificationTypes[i].value = parseInt(this.assetClassificationTypes[i].value)
            }
        }
        if (this.useStatusTypes.length > 0 && this.useStatusTypes !== null) {
            for (let i = 0; i < this.useStatusTypes.length; i++) {
                this.useStatusTypes[i].value = parseInt(this.useStatusTypes[i].value)
            }
        }
        if (this.acquisitionMethodTypes.length > 0 && this.acquisitionMethodTypes !== null) {
            for (let i = 0; i < this.acquisitionMethodTypes.length; i++) {
                this.acquisitionMethodTypes[i].value = parseInt(this.acquisitionMethodTypes[i].value)
            }
        }
        if (this.valueTypes.length > 0 && this.valueTypes !== null) {
            for (let i = 0; i < this.valueTypes.length; i++) {
                this.valueTypes[i].value = parseInt(this.valueTypes[i].value)
            }
        }
        if (this.ownershipCertificateTypes.length > 0 && this.ownershipCertificateTypes !== null) {
            for (let i = 0; i < this.ownershipCertificateTypes.length; i++) {
                this.ownershipCertificateTypes[i].value = parseInt(this.ownershipCertificateTypes[i].value)
            }
        }
        if (this.ownershipNatureTypes.length > 0 && this.ownershipNatureTypes !== null) {
            for (let i = 0; i < this.ownershipNatureTypes.length; i++) {
                this.ownershipNatureTypes[i].value = parseInt(this.ownershipNatureTypes[i].value)
            }
        }
        if (this.buildingStructureTypes.length > 0 && this.buildingStructureTypes !== null) {
            for (let i = 0; i < this.buildingStructureTypes.length; i++) {
                this.buildingStructureTypes[i].value = parseInt(this.buildingStructureTypes[i].value)
            }
        }
        // if (this.depreStatusTypes.length > 0 && this.depreStatusTypes !== null) {
        //     for (let i = 0; i < this.depreStatusTypes.length; i++) {
        //         this.depreStatusTypes[i].value = parseInt(this.depreStatusTypes[i].value)
        //     }
        // }
        // getAssetRegistrationList().then(res => {
        //     if (res.data.code === 0) {
        //         this.assetRegistrationTypes = res.data.data
        //     }
        // })
    },
    data() {
        return {
            governmentInvestmentFlag: false,
            showButton: false,
            fileList: [],
            modelType: 'propertyCard',
            projects: [],
            propForm: {
                assetFixedWarehousingId: '',
                cardType: '',
                cardNum: '',
                registrationPrice: '',
                registrationDate: '',
                projectId: '',
                landType: '',
                planningType: '',
                completionTime: '',
                obligee: '',
                assetCode: '',
                assetClassification: '',
                assetName: '',
                useStatus: '',
                acquisitionMethod: '',
                acquisitionDate: '',
                investmentAcquisitionDate: '',
                valueType: '',
                registrationFee: '',
                taxation: '',
                value: '',
                number: '',
                measurementUnit: '',
                landArea: '',
                propertyRightForm: '',
                ownershipCertificateNo: '',
                issueData: '',
                ownershipPeriod: '',
                ownershipCertificate: '',
                location: '',
                ownershipNature: '',
                selfUseArea: '',
                selfUseValue: '',
                buildingStructure: '',
                depreStatus: '',
                accumDepre: '',
                estimatedLife: '',
                initDepreMonth: '',
                lastDepreMonth: '',
                salvageValue: '',
                isCard: '2',
                landNumber: '',
                landAreaCard: '',
                assetRegistrationId: '',
                area: '',
                address: '',
                provinceCode: '',
                cityCode: '',
                countyCode: '',
                remarks: '',
                roomIds: [],
                warehousingStatus: '' //入库状态
            },
            // hxf-0918-不动产信息
            cardNum: '不动产证编号',
            assetClassificationTypes: [],
            useStatusTypes: [],
            acquisitionMethodTypes: [],
            valueTypes: [],
            ownershipCertificateTypes: [],
            ownershipNatureTypes: [],
            buildingStructureTypes: [],
            depreStatusTypes: [],
            cardTypes: [],
            isCards: [
                {
                    value: '1',
                    label: '否'
                },
                {
                    value: '2',
                    label: '是'
                }
            ],
            proprietaryTypes: [],
            planUses: [],
            landUses: [],
            assetRegistrationTypes:[]
        }
    },
    methods: {
        closeTab() {
               //关闭当前新增页，暂时没找到好办法
               this.$store.commit("delHistoryRoute", this.$route.fullPath);
               // this.$parent.$children[2].closeTab('current')
               //跳转
               sessionStorage.setItem('cardType',this.propForm.cardType)
               this.$router.push("/property-info-sys/proprietary-info");
        },
        handleDownload(fileId) {
            checkFiles(fileId).then(resp => {
                if (resp.data.code === 0) {
                    window.open('/admin/sys-file/download/' + fileId)
                } else {
                    this.$message({
                        message: `文件不存在！`,
                        type: 'warning'
                    })
                }
            })
        },
        getCardTypeValue(num, sum){
            let arr = sum.filter(e=>e.value === num)
            if (arr.length > 0) {
                return arr[0].label
            } else {
                return num
            }
        }
    },
    watch: {
        'propForm.cardType':function () {
            if (this.propForm !== null) {
                if (this.propForm.isCard === '' || this.propForm.isCard === null || this.propForm.isCard === undefined) {
                    this.$message.error('请先选择是否有不动产证！');
                    this.propForm.cardType = '';
                } else {
                    if (this.propForm.cardType === '20') {
                        this.cardNum = '移交协议编号'
                    } else if (this.propForm.cardType === '30') {
                        this.cardNum = '第三方评估机构报告编号'
                    } else {
                        this.cardNum = '不动产证编号'
                    }
                }
            }
        }
    }
};
